package cn.ztydata.teachingsystem.heaven.dao.redis;

import com.google.common.collect.Lists;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * 习题总数累计dao->redis类
 * Created by chenxin on 2015/2/10.
 */
@Repository
public class ExerciseCountDao {
    protected static Logger logger = LoggerFactory.getLogger(ExerciseCountDao.class); //日志记录器

    @Resource(name = "stringRedisTemplate")
    protected RedisTemplate redisTemplate; //sdr-Redis模版

    private static final String EXERCISE_TOTAL = "exercise:total";

    public static final String USE_TOTAL = "useTotal"; //使用次数 <=> 热度
    public static final String ANSWER_TOTAL = "answerTotal"; //答题次数
    public static final String RIGHT_TOTAL = "rightTotal"; //正确总数

    /**
     * 读取习题的某类总数
     * @param exerciseId 习题ID
     * @param field 字段名称
     * @return result 总数
     *
     * @author chenxin
     * @since 2015/2/10
     */
    public Long getTotal(final long exerciseId, final String field) {
        //获取hash操作
        HashOperations<String, String, String> hashOperations = redisTemplate.opsForHash();

        String result =  hashOperations.get(KeyUtils.generateKey(EXERCISE_TOTAL, exerciseId), field);

        logger.debug("读取习题的某类总数");
        return result == null ? 0 : Long.valueOf(result);
    }

    /**
     * 递增习题的某类总数
     * @param exerciseId 习题ID
     * @param field 字段名称
     * @return 总数
     *
     * @author chenxin
     * @since 2015/2/10
     */
    public Long increment(final long exerciseId, final String field) {
        //获取hash操作
        HashOperations<String, String, String> hashOperations = redisTemplate.opsForHash();

        return hashOperations.increment(KeyUtils.generateKey(EXERCISE_TOTAL, exerciseId), field, 1);
    }

    /**
     * 批量递增习题的某类总数
     * 
     * @param exerciseIds 习题ID集合
     * @param field 字段名称
     * 
     * @author cx
     * @since 2015/4/14
     */
    public void incrByBatch(final List<Long> exerciseIds, final String field) {
        //获取hash操作
        HashOperations<String, String, String> hashOperations = redisTemplate.opsForHash();

        logger.debug("批量递增习题的某类总数");
        for (long exerciseId : exerciseIds) {
            String key = KeyUtils.generateKey(EXERCISE_TOTAL, exerciseId);

            hashOperations.increment(key, field, 1);
        }
    }

    /**
     * 读取习题的多类总数
     * @param exerciseId 习题ID
     * @param fields 字段名称
     * @return List 总数集合
     *
     * @author chenxin
     * @since 2015/2/10
     */
    public List<Long> getMulti(final long exerciseId, final String... fields) {
        //获取hash操作
        HashOperations<String, String, String> hashOperations = redisTemplate.opsForHash();

        List<String> values = hashOperations.multiGet(KeyUtils.generateKey(EXERCISE_TOTAL, exerciseId), Arrays.asList(fields));

        //转换为long
        List<Long> totals = Lists.newArrayList();
        logger.debug("转换为long");
        for (String value : values) {
            totals.add(value == null ? 0 : Long.valueOf(value));
        }

        logger.debug("读取习题的多类总数");
        return totals;
    }
}
